package DataStructure.Tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryNode root = new BinaryNode(1, "D");
        BinaryNode left1 = new BinaryNode(2, "B");
        BinaryNode left2 = new BinaryNode(3, "A");
        BinaryNode right1 = new BinaryNode(4, "C");
        BinaryNode right2 = new BinaryNode(5, "E");
        root.setLeft(left1);
        left1.setLeft(left2);
        left1.setRight(right1);
        root.setRight(right2);
//        new BinaryTree(root).preOrder();
//        System.out.println();
        BinaryTree tree = new BinaryTree(root);
        tree.delete(6);
        System.out.println(tree.postOrderSearch(4));
//        System.out.println();
//        new BinaryTree(root).postOrder();
    }

}

class BinaryTree {
    private BinaryNode root;

    public BinaryTree(BinaryNode root) {
        this.root = root;
    }

    public void delete(int no) {
        if (root == null) {
            System.out.println("二叉树为空");
            return;
        }
        if (root.getNo() == no) {
            root = null;
        } else {
            root.delete(no);
        }
    }

    public void preOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
            return;
        }
        root.preOrder();
    }

    public BinaryNode preOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return root.preOrderSearch(no);
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
            return;
        }
        root.infixOrder();
    }

    public BinaryNode infixOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return root.infixOrderSearch(no);
    }

    /**
     * 后序相关
     */
    public void postOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
            return;
        }
        root.postOrder();
    }

    public BinaryNode postOrderSearch(int no) {
        if (root == null) {
            System.out.println("二叉树为空");
            return null;
        }
        return root.postOrderSearch(no);
    }
}

class BinaryNode {
    private int no;
    private String name;
    private BinaryNode left;
    private BinaryNode right;


    public void delete(int no) {
        //如果不加这个可能会报空指针异常
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        //而这里加了之后如果最终都为空，那么也没有递归了，方法就是直接结束
        if (this.left != null) this.left.delete(no);
        if (this.right != null) this.right.delete(no);
    }

    /**
     * 前 序 遍 历
     */
    public void preOrder() {
        System.out.print(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 前 序 查 找
     *
     * @param no
     * @return
     */
    public BinaryNode preOrderSearch(int no) {
        if (this.no == no) {
            return this;
        }
        BinaryNode res = null;
        if (this.left != null) {
            //如果不是第一次就找到的话，那么就需要这一步来得到然后再往下走返回
            res = this.left.preOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.preOrderSearch(no);
        }
        return res;
    }

    /**
     * 中 序 遍 历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.print(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 中 序 查 找
     */
    public BinaryNode infixOrderSearch(int no) {
        BinaryNode res = null;
        if (this.left != null) {
            res = this.left.infixOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            res = this.right.infixOrderSearch(no);
        }
        return res;
    }

    /**
     * 后 序 遍 历
     */
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.print(this);
    }

    /**
     * 后 序 查 找
     */
    public BinaryNode postOrderSearch(int no) {
        BinaryNode res = null;
        if (this.left != null) {
            res = this.left.postOrderSearch(no);
        }
        if (res != null) {
            return res;
        }
        if (this.right != null) {
            res = this.right.postOrderSearch(no);
        }
        if (this.no == no) {
            return this;
        }
        return res;
    }

    public BinaryNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BinaryNode getLeft() {
        return left;
    }

    public void setLeft(BinaryNode left) {
        this.left = left;
    }

    public BinaryNode getRight() {
        return right;
    }

    public void setRight(BinaryNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return name;


    }
}